package cn.wqb.bpp.common.utils;

import org.lionsoul.ip2region.DataBlock;
import org.lionsoul.ip2region.DbConfig;
import org.lionsoul.ip2region.DbSearcher;
import org.lionsoul.ip2region.Util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;

/**
 * 依赖库
 * <dependency>
 * <groupId>org.lionsoul</groupId>
 * <artifactId>ip2region</artifactId>
 * <version>1.7.2</version>
 * </dependency>
 */

public class CityUtils {

    /**
     * 根据IP获得城市
     *
     * @param ip
     * @return
     */
    public static String getCityByIP(String ip) {
        try {
            String dbPath = CityUtils.class.getResource("/ip2region.db").getPath();
            System.out.println("ip2region.db path:" + dbPath);
            File file = new File(dbPath);
            if (file.exists() == false) {
                String tmpDir = System.getProperties().getProperty("java.io.tmpdir");
                dbPath = tmpDir + "ip.db";
                System.out.println("ip2region.db path:" + dbPath);
                file = new File(dbPath);
                copyInputStreamToFile(CityUtils.class.getClassLoader().getResourceAsStream("classpath:ip2region.db"), file);
            }
            int algorithm = DbSearcher.BTREE_ALGORITHM;
            try {
                DbConfig config = new DbConfig();
                DbSearcher searcher = new DbSearcher(config, dbPath);

                //define the method
                Method method = null;
                switch (algorithm) {
                    case DbSearcher.BTREE_ALGORITHM:
                        method = searcher.getClass().getMethod("btreeSearch", String.class);
                        break;
                    case DbSearcher.BINARY_ALGORITHM:
                        method = searcher.getClass().getMethod("binarySearch", String.class);
                        break;
                    case DbSearcher.MEMORY_ALGORITYM:
                        method = searcher.getClass().getMethod("memorySearch", String.class);
                        break;
                }
                DataBlock dataBlock = null;
                if (Util.isIpAddress(ip) == false) {
                    System.out.println("Error: Invalid ip address");
                }
                dataBlock = (DataBlock) method.invoke(searcher, ip);
                String fullInfo = dataBlock.getRegion();
                //System.out.println("fullInfo:" + fullInfo);
                String[] fullInfoArr = fullInfo.split("\\|");
                return fullInfoArr[3];
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static boolean copyInputStreamToFile(InputStream inputStream, File file) {
        try {
            byte[] buffer = new byte[inputStream.available()];
            inputStream.read(buffer);
            OutputStream outStream = new FileOutputStream(file);
            outStream.write(buffer);
            outStream.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

}
